package com.dubito.tmdbmovie.ui.component.appbar

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Clear
import androidx.compose.material.icons.filled.Search
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import com.dubito.tmdbmovie.R
import com.dubito.tmdbmovie.ui.screen.main.MainViewModel


@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SearchBar(isAppBarVisible: MutableState<Boolean>, viewModel: MainViewModel) {
    var text by remember { mutableStateOf("") }
    val focusRequester = FocusRequester()
    BackHandler(isAppBarVisible.value.not()) {
        isAppBarVisible.value = true
    }
    Column(modifier = Modifier.padding(top = 34.dp)) {

        BasicTextField(
            value = text,
            onValueChange = {
                text = it
                viewModel.searchApi(it)
            },
            decorationBox = { innerTextField ->
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.padding(vertical = 2.dp, horizontal = 8.dp)
                ) {
                    Icon(
                        imageVector = Icons.Filled.Search,
                        contentDescription = "search",
                        tint = Color.Gray
                    )
                    Box(
                        modifier = Modifier
                            .padding(horizontal = 10.dp)
                            .weight(1F),
                        contentAlignment = Alignment.CenterStart
                    ) {
                        if (text.isEmpty()) {
                            Text(text = "请输入关键字", style = TextStyle(color = Color.Gray))
                        }
                        innerTextField()
                    }
                    if (text.isNotEmpty()) {
                        IconButton(onClick = { text = "" }, modifier = Modifier.size(16.dp)) {
                            Icon(imageVector = Icons.Filled.Clear, contentDescription = "clear")
                        }
                    }
                }

            },
            modifier = Modifier
                .focusRequester(focusRequester)
                .padding(horizontal = 10.dp)
                .background(Color.White, CircleShape)
                .height(30.dp)
                .fillMaxWidth()
        )
//        TextField(
//            modifier = Modifier
//                .fillMaxWidth()
//                .focusRequester(focusRequester),
//            value = text,
////            label = {
////                    Text(text = stringResource(R.string.input_value))
////            },
//            colors = TextFieldDefaults.textFieldColors(
//                cursorColor = Color.Black,
//                focusedIndicatorColor = Color.Transparent,
//                unfocusedIndicatorColor = Color.Transparent
//            ),
//            onValueChange = {
//                text = it
//                viewModel.searchApi(it)
//            },
//            shape = RoundedCornerShape(8.dp),
//            singleLine = true,
//            trailingIcon = {
//                if (text.trim().isNotEmpty()) {
//                    Icon(
//                        Icons.Filled.Clear,
//                        contentDescription = "clear text",
//                        modifier = Modifier
//                            .padding(end = 16.dp)
//                            .offset(x = 10.dp)
//                            .clickable { text = "" }
//                    )
//                } else {
//                    Icon(
//                        Icons.Filled.Search,
//                        contentDescription = "search",
//                        modifier = Modifier
//                            .padding(end = 16.dp)
//                            .offset(x = 10.dp)
//                            .clickable {}
//                    )
//                }
//            }
//        )
        LaunchedEffect(Unit) {
            focusRequester.requestFocus()
        }
    }

}